#include "client.h"

int TCP_Client_init(char const *port_num, char const *IP_addr) // 初始化TCP套接字
{
    int sockfd;
    struct sockaddr_in server_addr;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
    }

    memset(&server_addr, 0 , sizeof(server_addr)); // 清零
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(atoi(port_num));
    if (inet_pton(AF_INET, IP_addr, &server_addr.sin_addr) <= 0)
    {
        perror("inet_pton");
        exit(1);
    }

    if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("connect");
        exit(1);
    }

    return sockfd;
}